<template>
  <div class="oper-wrapper">
    <!-- <div class="oper-item square">Fold</div> -->
    <div class="oper-item photo" v-if="photo" :title="userInfo.username">
      <img :src="photo" />
      <span>{{userInfo.username}}</span>
    </div>
    <!-- <div class="oper-item fold-btn color-red" title="折叠面板" @click="fold">折叠</div>
    <div class="fold-show-btn" title="展开面板" @click="showFold">展开</div> -->
    <!-- <div class="oper-item" title="个人信息">Info</div>
    <div class="oper-item">设置</div> -->
    <div class="oper-item">
      <label for="file">传头像</label>
      <input id="file" style="display:none;" type="file" @change="uploadUserPhoto" />
    </div>

    <div class="oper-item" @click="logout">
      退出
    </div>
  </div>
</template>

<script>
export default {
  name: 'Oper',
  data() {
    return {
      userInfo: {},
      photo: '',
      account: ''
    }
  },
  mounted() {
    this.userInfo = JSON.parse(this.$storage.getItem('userinfo'));
    this.account = this.userInfo.id;
    this.photo = this.userInfo.photo;
  },
  methods: {
    uploadUserPhoto(event) {
      const file = event.target.files[0];
      const type = file.type.split('/')[0];
      if (type !== 'image') {
        alert('请上传图片');
        return;
      }
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = () => {
        this.photo = reader.result;
        this.$axios.post('/users/editoruserphoto', {
          id: this.userInfo.id,
          photo: reader.result
        }).then(res => {
          if (res.data.code === 200) {
            this.userInfo.photo = reader.result;
            this.$storage.setItem('userinfo', JSON.stringify(this.userInfo));
            this.$bus.publisher('update_user_info')
            alert('上传成功');
          } else {
            alert('上传失败');
          }
        })

      }
    },
    fold() {
      document.getElementsByClassName('oper-wrapper')[0].className = 'oper-wrapper fold';
      document.getElementsByClassName('fold-show-btn')[0].style.display = 'block';
    },
    showFold() {
      document.getElementsByClassName('fold-show-btn')[0].style.display = 'none';
      document.getElementsByClassName('oper-wrapper')[0].className = 'oper-wrapper';
    },
    async logout() {
      await this.emitClose();
      setTimeout(() => {
        this.$storage.clear([
          'userinfo',
          'account',
          'index'
        ]);
        this.$axios.get(`/users/logout?id=${this.account}`);
        this.$router.push('/login');
      }, 500);
    },
    async emitClose() {
      return new Promise((resolve) => {
        this.$emit('close_socket', true);
        resolve();
      })
    }
  }
}
</script>

<style scoped>
.oper-wrapper {
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 50px;
  height: 100%;
  border-right: 1px solid #ccc;
  text-align: center;
  padding-top: 5px;
  transition: all 0.5s ease-in-out;
}

.oper-wrapper.fold {
  width: 0;
}

.oper-item {
  width: 40px;
  height: 40px;
  line-height: 40px;
  margin-bottom: 5px;
  border-radius: 50%;
  border: 1px solid #ccc;
  color: #ffffff;
  font-size: 12px;
  cursor: pointer;
}

.oper-item.color-red {
  color: #ffffff;
  background-color: red;
  border: none !important;
}

.oper-item.square {
  border-radius: 0 !important;
}

.oper-item.photo {
  position: absolute;
  top: 10px;
  height: 60px;
  border: none;
  display: flex;
  flex-direction: column;
}
.oper-item span {
  display: inline-flex;
  flex: 1;
  width: 40px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.oper-item img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-bottom: 5px;
  cursor: pointer;
  object-fit: cover;
}

.fold-show-btn {
  width: 40px;
  line-height: 40px;
  height: 40px;
  border-radius: 50%;
  margin-bottom: 5px;
  color: #ffffff;
  font-size: 12px;
  cursor: pointer;
  position: fixed;
  top: 50%;
  left: 0;
  margin-top: -65px;
  display: none;
  transition: all 0.5s ease-in-out;
  background-color: red;
  box-sizing: content-box;
}
</style>